﻿namespace test3

open System
open System.Management

module wmi =
    type WmiMethodInvoker(instance:ManagementObject) =
        member private x.instance = instance
        static member (?) (this:WmiMethodInvoker, methname:string): (obj array->'a) =
            fun args -> unbox (this.instance.InvokeMethod(methname,args))

    type WmiAssociators(instance:ManagementObject) =
        member private x.instance = instance
        static member (?) (this:WmiAssociators, sel:string) =
            match sel.Split(':') with
            | [| resultcls; assoccls |] -> this.instance.GetRelated(resultcls) |> Seq.cast |> Seq.map(fun x->new WmiObj(x))
            | [| resultcls |] -> this.instance.GetRelated(resultcls) |> Seq.cast |> Seq.map(fun x->new WmiObj(x))
            | _ -> Seq.empty
    and WmiObj (instance:ManagementObject) =
        member private x.instance = instance
        static member (?) (this:WmiObj, propname:string) =
            unbox (this.instance.[propname])
        static member (?<-) (this:WmiObj, propname:string, propval) =
            this.instance.[propname] <- propval
        member x.invoke = new WmiMethodInvoker(instance)
        member x.assoc = new WmiAssociators(instance)
        member x.text = x.instance.GetText(TextFormat.CimDtd20)
        member x.path = x.instance.Path.Path

    type WmiEnum (prefix:string) =
        member private x.prefix = prefix
        static member (?) (this:WmiEnum, clsname:string) =
            let cls = new ManagementClass(this.prefix+clsname)
            cls.GetInstances() |> Seq.cast |> Seq.map(fun x->new WmiObj(x))

